<?php
namespace app\shop\home;
use think\Cache;
use think\captcha\Captcha;
use app\shop\model\User;
use think\Session;
use app\common\builder\Mail;

/**
 * 前台首页控制器
 */
class Login extends Common
{

    public function index()
    {
        if(session("auction_user") != ''){
            $this->redirect('shop/index/index');
        }
    	if(request()->isAjax()){
    		extract(request()->param());
            //$captchas = new Captcha();
            //$captchas->entry();
            //print_r(input('session.'));exit;
			//if(!$captchas->check($code)){
               // $this->error('验证码不正确');
           // }
			//执行登录操作
			$user = User::where(['mobile'=>$mobile,'password'=>md5(md5($password))])->find();
			if ($user) {
                session("auction_user", $user);
                return json(['code'=>0, 'msg'=>'登录成功']);
            }else{
                return json(['code'=>1001, 'msg'=>'登录信息错误！！！']);
            }

    	}
    	$url = request()->header('referer');
    	$this->assign('url',$url);
        if(isMobile()){
        
            return $this->fetch('Mobile/login/index');
       }else{
            return $this->fetch();
       }
        
    }

    public function register()
    {
    	if(request()->isAjax()){
    		extract(request()->param());
            $isReg = User::where('mobile',$phone)->find();
            if($isReg) return json(['code'=>1001, 'msg'=>'注册失败，该账号已被注册']);
    		$user = User::create([
    					'mobile'=>$phone,
    					'password' => md5(md5($password)),
    				]);

    		if($user) return json(['code'=>0, 'msg'=>'注册成功']);
    	}
    	if(isMobile()){
        
            return $this->fetch('Mobile/login/register');
       }else{
            return $this->fetch();
       }
    }
    public function editfind()
    {
    	if(request()->isAjax()){
    		extract(request()->param());
            $isReg = User::where('mobile',$phone)->find();
            if(!$isReg) return json(['code'=>1001, 'msg'=>'找不到手机号信息，请重新确认']);
    		$user = User::where([
    					'mobile'=>$phone,

    				])->update(['password' => md5(md5($password))]);

    		if($user !== false) {
                return json(['code'=>0, 'msg'=>'修改密码成功']);
            }else{
                return json(['code'=>1001, 'msg'=>'修改密码失败']);
            };
    	}
    	if(isMobile()){
            return $this->fetch('Mobile/login/register');
       }else{
            return $this->fetch();
       }
    }
    public function read(){
        if(isMobile()){

            return $this->fetch('Mobile/login/read');
        }else{
            return $this->fetch();
        }
    }


    public function registerEmail()
    {
        if(request()->isAjax()){
            extract(request()->param());
            //查询该邮箱是否被注册
            $isReg = User::where('email',$email)->find();
            if($isReg) return json(['code'=>1001, 'msg'=>'注册失败，该账号已被注册']);
            
            $user = User::create([
                        'email'=>$email,
                        'password' => md5(md5($password)),
                    ]);

            if($user) return json(['code'=>0, 'msg'=>'注册成功']);
        }
    	return $this->fetch();
    }

    /*
    * 发送手机验证码
    */
    public function sendcode()
    {
    	$phone = request()->param('phone');
    	$code = rand(1111,9999);
    	Cache::set('reg_code@'.$phone, $code, 2000);
    	sendSms($phone,$code);
    	echo 1;exit;
    	//echo $code;
    }/*
    * 发送手机验证码
    */
    public function sendfindcode()
    {
    	$phone = request()->param('phone');
    	if(!$phone){
            return json(['code'=>0, 'msg'=>'请输入手机号']);
        }
        $has = User::where('mobile',$phone)->find();
        if(!$has){
            return json(['code'=>0, 'msg'=>'手机号不存在']);
        }
    	$code = rand(1111,9999);
    	Cache::set('find_code@'.$phone, $code, 2000);
    	sendSms($phone,$code);
    	//echo $code;
        return json(['code'=>1, 'msg'=>'发送成功']);
    }

    /*
    * 获取手机验证码
    */
    public function getcode()
    {
    	$phone = request()->param('phone');
    	$code = intval(request()->param('code'));
    	if($code === Cache::get('reg_code@'.$phone)){
    		return json(['code'=>0, 'msg'=>'success']);
    	}else{
    		return json(['code'=>1001, 'msg'=>'验证码不匹配']);
    	}
    }

    //发送邮箱验证码
    public function sendEmailCode()
    {
        $email = request()->param('email');
        $code = rand(1111,9999);
        Cache::set('reg_code_email@'.$email, $code, 2000);
        //邮箱发送验证码
       /* $mail = new Mail();
        $mail->setServer("smtp.qq.com", "850616840@qq.com", "ilgrlfzdcffubdhc", 465);
        $mail->setFrom("itpointerme");
        $mail->setReceiver($email);
        $mail->setMailInfo("登录验证码", "<b>登录验证码:</b>".$code);
        if($mail->sendMail()){
            return json(['code'=>0, 'msg'=>'success']);
        }else{
            return json(['code'=>1001, 'msg'=>'error']);
        }*/
        sendEmail($email,'测试当前验证码为'.$code);
        //echo $code;

    }

    /*
    * 获取邮箱验证码
    */
    public function getEmailCode()
    {
        $email = request()->param('email');
        $code = intval(request()->param('code'));
        if($code === Cache::get('reg_code_email@'.$email)){
            return json(['code'=>0, 'msg'=>'success']);
        }else{
            return json(['code'=>1001, 'msg'=>'验证码不匹配']);
        }
    }

    /**
    * 退出登录
    */
    public function logout()
    {

        session('auction_user', null);
        $this->redirect('shop/index/index');
    }
    /**
     *
     * 找回密码
     */
    public function find(){

        if(isMobile()){

            return $this->fetch('Mobile/login/find');
        }else{
            return $this->fetch();
        }
    }

    /*
    * 获取手机验证码
    */
    public function getfindcode()
    {
        $phone = request()->param('phone');
        $code = intval(request()->param('code'));
        if($code === Cache::get('find_code@'.$phone)){
            return json(['code'=>0, 'msg'=>'success']);
        }else{
            return json(['code'=>1001, 'msg'=>'验证码不匹配']);
        }
    }
}